package com.ruoyi.merchant.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import java.util.Hashtable;
import java.util.List;
import java.util.UUID;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.file.utils.OSSUtils;
import com.ruoyi.file.utils.QRcode;
import com.ruoyi.file.utils.UUIDCode;
import com.ruoyi.merchant.service.FileQR;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.ShopRepairReservationMapper;
import com.ruoyi.merchant.domain.ShopRepairReservation;
import com.ruoyi.merchant.service.IShopRepairReservationService;

/**
 * 维修任务管理Service业务层处理
 * 
 * @author Wevon
 * @date 2024-09-12
 */
@Service
public class ShopRepairReservationServiceImpl implements IShopRepairReservationService 
{
    @Autowired
    private ShopRepairReservationMapper shopRepairReservationMapper;
    @Autowired
    private FileQR fileQR;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 查询维修任务管理
     * 
     * @param id 维修任务管理主键
     * @return 维修任务管理
     */
    @Override
    public ShopRepairReservation selectShopRepairReservationById(Long id)
    {
        return shopRepairReservationMapper.selectShopRepairReservationById(id);
    }

    /**
     * 查询维修任务管理列表
     * 
     * @param shopRepairReservation 维修任务管理
     * @return 维修任务管理
     */
    @Override
    public List<ShopRepairReservation> selectShopRepairReservationList(ShopRepairReservation shopRepairReservation)
    {
        return shopRepairReservationMapper.selectShopRepairReservationList(shopRepairReservation);
    }

    /**
     * 根据用户ID查询维修任务管理列表
     *
     * @param userId 维修任务管理
     * @return 维修任务管理
     */
    @Override
    public List<ShopRepairReservation> selectShopRepairReservationByUserId(Long userId)
    {
        return shopRepairReservationMapper.selectShopRepairReservationByUserId(userId);
    }
    /**
     * 新增维修任务管理
     * 
     * @param shopRepairReservation 维修任务管理
     * @return 结果
     */
    @SneakyThrows
    @Override
    public Long insertShopRepairReservation(ShopRepairReservation shopRepairReservation)
    {
        //生成一个短的UUID,作为核销码
        String VerificationCode = UUIDCode.generateShortUuid();
        shopRepairReservation.setVerificationCode(VerificationCode);
        String text =VerificationCode; // 二维码内容
        int width = 300; // 二维码图片宽度
        int height = 300; // 二维码图片高度
        String format = "jpg";// 二维码的图片格式
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        // 生成二维码
        File outputFile = new File("d:" + File.separator + "new.jpg");
        QRcode.writeToFile(bitMatrix, format, outputFile);
        /*解决重名问题*/
        String fileName = UUID.randomUUID()+".jpg";
        //当前日期文件夹
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateDirPath = sdf.format(new Date());
        String url = "";
        url = OSSUtils.upload(dateDirPath+"/"+fileName,new FileInputStream(outputFile));
        shopRepairReservation.setVerificationImg(url);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.now();
        String formattedDateTime = dateTime.format(formatter);
        shopRepairReservation.setSubmitTime(formattedDateTime);
        shopRepairReservationMapper.insertShopRepairReservation(shopRepairReservation);
        Long returnId = shopRepairReservation.getId();
        return returnId;
    }

    /**
     * 修改维修任务管理
     * 
     * @param shopRepairReservation 维修任务管理
     * @return 结果
     */
    @Override
    public int updateShopRepairReservation(ShopRepairReservation shopRepairReservation)
    {
        return shopRepairReservationMapper.updateShopRepairReservation(shopRepairReservation);
    }

    /**
     * 批量删除维修任务管理
     * 
     * @param ids 需要删除的维修任务管理主键
     * @return 结果
     */
    @Override
    public int deleteShopRepairReservationByIds(Long[] ids)
    {
        return shopRepairReservationMapper.deleteShopRepairReservationByIds(ids);
    }

    /**
     * 删除维修任务管理信息
     * 
     * @param id 维修任务管理主键
     * @return 结果
     */
    @Override
    public int deleteShopRepairReservationById(Long id)
    {
        return shopRepairReservationMapper.deleteShopRepairReservationById(id);
    }
}
